{% load static %}
<!DOCTYPE html>
<html class="x-admin-sm">

<head>
    <meta charset="UTF-8">
    <title>管理员列表</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi"/>
    <link rel="stylesheet" href="/static/css/font.css">
    <link rel="stylesheet" href="/static/css/xadmin.css">
    <script src="/static/lib/layui/layui.js" charset="utf-8"></script>
    <script type="text/javascript" src="/static/js/xadmin.js"></script>
    <script type="text/javascript" src="/static/js/jquery-3.2.1.min.js"></script>
    <script type="text/javascript" src="/static/js/jquery.cookie.js"></script>
    <script src="/static/js/flow-meter.js" charset="utf-8"></script>
    <!--[if lt IE 9]>
          <script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>
          <script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>
        <![endif]-->
</head>

<body>
<div class="x-nav">
            <span class="layui-breadcrumb">
                <a href="">首页</a>
                <a href="">
                    <cite>管理员列表</cite>
                </a>
            </span>
    <a class="layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right"
       onclick="location.reload()" title="刷新">
        <i class="layui-icon layui-icon-refresh" style="line-height:30px"></i>
    </a>
</div>
<div class="layui-fluid">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-card-body ">
                    <form class="layui-form layui-col-space5">
                        <div class="layui-inline layui-col-md1">
                            <input class="layui-input" autocomplete="off" placeholder="开始日" name="begin_time"
                                   id="begin_time">
                        </div>
                        <div class="layui-inline layui-col-md1">
                            <input class="layui-input" autocomplete="off" placeholder="截止日" name="end_time"
                                   id="end_time">
                        </div>
                        <div class="layui-inline layui-col-md2">
                            <input type="text" name="query_box" placeholder="请输入用户名\联系电话\邮箱" autocomplete="off"
                                   class="layui-input" id="query_box">
                        </div>
                        <div class="layui-inline layui-show-xs-block">
                            <button class="layui-btn" lay-submit="" lay-filter="search">
                                <i class="layui-icon">&#xe615;</i>
                            </button>
                        </div>
                    </form>
                </div>
                <div class="layui-card-body ">
                    <table lay-filter="admin" id="admin">
                    </table>
                </div>
            </div>
        </div>
    </div>
</div>
</body>
<script type="text/html" id="toolbarDemo">
    <button class="layui-btn layui-btn-danger" lay-event="del_batch">
        <i class="layui-icon"></i>批量删除
    </button>
    <button class="layui-btn" onclick="xadmin.open('添加管理员','/admin/add/',600,400)">
        <i class="layui-icon"></i>添加
    </button>
    <button class="layui-btn layui-btn-sm" onclick="xadmin.open('导入管理员','/admin/import/',400,200)">导入</button>
    <button class="layui-btn layui-btn-sm" lay-event="export">导出</button>
</script>
{% verbatim %}
<script type="text/html" id="displayOmit">
    <div><span title="{{d.remark}}">{{d.remark}}</span></div>
</script>
{% endverbatim %}
<script>

    layui.use('laydate', function () {
        var laydate = layui.laydate;

        //执行一个laydate实例
        laydate.render({
            elem: '#begin_time' //指定元素
        });

        //执行一个laydate实例
        laydate.render({
            elem: '#end_time', //指定元素
        });

    });
</script>
<script>
    layui.use(['table', 'form', 'jquery', 'element'],
        function () {
            $ = layui.jquery;
            table = layui.table;
            form = layui.form;

            table.render({
                elem: '#admin', //指定原始表格元素选择器（推荐id选择器）
                cols: [[
                    {checkbox: true, width: '5%', field: 'id'},
                    {
                        numbbers: true,
                        width: '5%',
                        title: '序号',
                        templet: function (data) {
                            return data.LAY_INDEX;
                        }
                    },
                    {field: 'name', edit: 'text', width: '15%', title: '用户名'},
                    {field: 'phone', edit: 'text', width: '10%', title: '联系电话'},
                    {field: 'email', edit: 'text', width: '20%', title: '邮箱'},
                    {field: 'remark', edit: 'text', width: '25%', templet: '#displayOmit', title: '备注'},
                    {field: 'create_time', width: '10%', title: '创建时间'},
                    {
                        field: 'state',
                        templet: '#switchState',
                        width: '10%',
                        title: '状态',
                        templet: function (data) {
                            if (data.state === '启用') {
                                return "<form>\n" +
                                    "        <input type=\"checkbox\" name=\"state\" value=\"" + data.id + "\" lay-skin=\"switch\" lay-text=\"启用|禁用\" lay-filter=\"state\" checked>\n" +
                                    "    </form>";
                            } else {
                                return "<form>\n" +
                                    "        <input type=\"checkbox\" name=\"state\" value=\"" + data.id + "\" lay-skin=\"switch\" lay-text=\"启用|禁用\" lay-filter=\"state\">\n" +
                                    "    </form>";
                            }
                        }
                    },
                ]],
                url: '/handler/admin/?action=query_admin',
                page: true,
                toolbar: '#toolbarDemo',
                defaultToolbar: ['filter', 'print'],

            });
            form.on('switch(state)', function (data) {
                post({
                    "param": JSON.stringify({'admin_id': data.value}),
                    "action": "switch_admin_state"
                }, '/handler/admin/', function (data) {
                    if (!data.success) {
                        layer.msg(data.msg, {icon: 5, time: 1000});
                    }
                });
            });
            //监听单元格编辑
            table.on('edit(admin)',
                function (obj) {
                    let value = obj.value, data = obj.data, field = obj.field;
                    delete data.create_time;
                    delete data.role;
                    post({
                        "param": JSON.stringify(data),
                        "action": "edit_admin"
                    }, '/handler/admin/', function (data) {
                        if (!data.success) {
                            layer.msg(data.msg, {icon: 5, time: 2000});
                        }
                    });
                });

            //头工具栏事件
            table.on('toolbar(admin)',
                function (obj) {
                    let checkStatus = table.checkStatus('admin');
                    let ids = [];
                    for (let i = 0; i < checkStatus.data.length; i++) {
                        ids.push(checkStatus.data[i].id);
                    }
                    switch (obj.event) {
                        case 'del_batch':
                            layer.confirm('确认要删除吗？', function (index) {
                                post({
                                    "param": JSON.stringify({'admin_ids': ids}),
                                    "action": "del_batch_admin"
                                }, '/handler/admin/', function (data) {
                                    if (!data.success) {
                                        layer.msg(data.msg, {icon: 5, time: 1000});
                                    } else {
                                        layer.msg('删除成功!', {icon: 1});
                                        table.reload('admin');
                                    }
                                });
                            });
                            break;
                        case 'export':
                            post({
                                "param": JSON.stringify({'admin_ids': ids}),
                                "action": "export_admin"
                            }, '/handler/admin/', function (res) {
                                if (!res.success) {
                                    layer.msg(res.msg, {icon: 5, time: 3000});
                                } else {
                                    let param = JSON.stringify({
                                        "filename": res.data,
                                    });
                                    let action = 'download_file';
                                    window.open('/handler/file/?param=' + param + '&action=' + action);
                                }
                            });
                            break;
                    }
                });
            //监听提交
            form.on('submit(search)',
                function (data) {
                    table.reload('admin', {
                        "where": {
                            "param": JSON.stringify(data.field),
                        }
                    });
                    return false;
                });
        });
</script>
<script>var _hmt = _hmt || [];
(function () {

})();</script>

</html>